"""
PRD v0.3.6 最终验证脚本
全面验证枢轴点算法和止盈止损重新计算
"""

def verify_complete_implementation():
    """全面验证PRD v0.3.6实现"""

    print("=" * 70)
    print("PRD v0.3.6 完整实现验证")
    print("=" * 70)

    # 读取代码
    with open('/mnt/i/projects/marketStream/src/marketStream/services/backtesting_service_v9.py', 'r') as f:
        code = f.read()

    # 检查列表
    checks = {
        "枢轴点算法": "枢轴点算法：交易员常用的专业工具",
        "P计算": "pivot_point = (prev_high + prev_low + prev_close) / 3",
        "S1计算": "s1 = (pivot_point * 2) - prev_high",
        "R1计算": "r1 = (pivot_point * 2) - prev_low",
        "breakoutPrice选择": "breakout_price = r1 if action in ['long', 'Long', 'LONG'] else s1",
        "S2计算": "s2 = pivot_point - (prev_high - prev_low)",
        "R2计算": "r2 = pivot_point + (prev_high - prev_low)",
        "做多measureMove": "recalculated_measure_move = breakout_price + range_distance",
        "做空measureMove": "recalculated_measure_move = breakout_price - range_distance",
        "stopLoss重新计算": "recalculated_stop_loss = s1",
        "takeProfit重新计算": "recalculated_take_profit = r2",
        "覆盖AI建议": "measure_move = recalculated_measure_move",
        "DEBUG输出": "DEBUG PRD v0.3.6",
    }

    print("\n📋 代码检查：")
    all_passed = True
    for check_name, pattern in checks.items():
        if pattern in code:
            print(f"  ✅ {check_name}")
        else:
            print(f"  ❌ {check_name}")
            all_passed = False

    # 手动验证算法
    print("\n" + "=" * 70)
    print("算法验证")
    print("=" * 70)

    test_data = [
        {"high": 100, "low": 90, "close": 95, "action": "long"},
        {"high": 100, "low": 90, "close": 95, "action": "short"},
        {"high": 200, "low": 180, "close": 190, "action": "long"},
    ]

    for i, data in enumerate(test_data, 1):
        print(f"\n🧪 测试用例 {i}：")
        print(f"  数据: High={data['high']}, Low={data['low']}, Close={data['close']}")
        print(f"  动作: {data['action']}")

        # 计算枢轴点
        prev_high = data["high"]
        prev_low = data["low"]
        prev_close = data["close"]
        action = data["action"]

        pivot_point = (prev_high + prev_low + prev_close) / 3
        s1 = (pivot_point * 2) - prev_high
        r1 = (pivot_point * 2) - prev_low
        s2 = pivot_point - (prev_high - prev_low)
        r2 = pivot_point + (prev_high - prev_low)
        range_distance = prev_high - prev_low

        print(f"  枢轴点: P={pivot_point:.2f}, S1={s1:.2f}, S2={s2:.2f}, R1={r1:.2f}, R2={r2:.2f}")

        # 应用算法
        if action in ['long', 'Long', 'LONG']:
            breakout = r1
            measure_move = breakout + range_distance
            stop_loss = s1
            take_profit = r2
        else:
            breakout = s1
            measure_move = breakout - range_distance
            stop_loss = r1
            take_profit = s2

        print(f"  结果:")
        print(f"    breakoutPrice={breakout:.2f}")
        print(f"    measureMove={measure_move:.2f}")
        print(f"    stopLoss={stop_loss:.2f}")
        print(f"    takeProfit={take_profit:.2f}")

        # 验证逻辑
        if action in ['long', 'Long', 'LONG']:
            valid = breakout > stop_loss and take_profit > breakout and measure_move > take_profit
        else:
            valid = stop_loss > breakout and breakout > take_profit and breakout > measure_move

        print(f"  验证: {'✅ 通过' if valid else '❌ 失败'}")

    # 总结
    print("\n" + "=" * 70)
    if all_passed:
        print("✅ 所有检查通过 - PRD v0.3.6 实现正确")
    else:
        print("❌ 部分检查失败 - 请检查代码")
    print("=" * 70)

    # 优势说明
    print("\n🎯 新方案优势：")
    print("  1. ✅ 使用专业枢轴点算法")
    print("  2. ✅ breakoutPrice基于R1/S1，精确可靠")
    print("  3. ✅ measureMove基于枢轴点和波动区间")
    print("  4. ✅ stopLoss基于S1/R1，科学合理")
    print("  5. ✅ takeProfit基于S2/R2，目标明确")
    print("  6. ✅ 所有价格基于同一系统，逻辑一致")
    print("  7. ✅ 避免AI建议与实际开仓价冲突")

    print("\n📊 对比：")
    print("  旧方案: AI识别breakoutPrice + AI建议止盈止损 (可能不一致)")
    print("  新方案: 枢轴点计算breakoutPrice + 基于枢轴点计算止盈止损 (完全一致)")

    return all_passed

if __name__ == "__main__":
    success = verify_complete_implementation()
    print("\n" + "=" * 70)
    if success:
        print("🎉 PRD v0.3.6 完整实现验证成功！")
    else:
        print("⚠️  存在未通过的检查项")
    print("=" * 70)